package ldh.im.fxbase.pojo;

import ldh.im.dto.business.P2psTextMessage;
import ldh.im.fxbase.cache.SoftId;
import ldh.im.fxbase.pojo.ext.MessageExt;
import ldh.im.fxbase.pojo.ext.MessageStatus;
import ldh.im.util.JsonUtil;

import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;

public class DbpsTextMessage extends P2psTextMessage implements SoftId {

    private Long id;
    private Long version;
    private String serverId;
    private User fromUser;
    private Set<User> toUserSet;
    private MessageStatus status;
    private Integer groupId;
    private Date updateTime;  // 消息创建时间
    private String ext;
    private MessageExt messageExt;

    private transient ImGroup imGroup;

    public DbpsTextMessage() {}

    @Override
    public String getSoftId() {
        return getLocalSeq() != null ? getLocalSeq() + "" : "";
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public ImGroup getImGroup() {
        return imGroup;
    }

    public void setImGroup(ImGroup imGroup) {
        this.imGroup = imGroup;
    }

    public User getFromUser() {
        return fromUser;
    }

    public void setFromUser(User fromUser) {
        this.fromUser = fromUser;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public MessageExt getMessageExt() {
        if (messageExt == null) {
            messageExt = new MessageExt();
        }
        return messageExt;
    }

    public void setMessageExt(MessageExt messageExt) {
        this.messageExt = messageExt;
    }

    public void initMessageExt() {
        if (ext != null && !ext.trim().equals("")) {
            messageExt = JsonUtil.toObject(ext, MessageExt.class);
        }
    }

    public void initExt() {
        if(messageExt != null) {
            ext = JsonUtil.toJson(messageExt);
        }
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public MessageStatus getStatus() {
        return status;
    }

    public void setStatus(MessageStatus status) {
        this.status = status;
    }

    public Set<User> getToUserSet() {
        return toUserSet;
    }

    public void setToUserSet(Set<User> toUserSet) {
        this.toUserSet = toUserSet;
    }

    public Set<Long> getToUserIds() {
        if (toUserSet != null) {
            return toUserSet.stream().map(User::getId).collect(Collectors.toSet());
        }
        return super.getToUserIds();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
        this.setLocalSeq("pc_" + id);
    }
}
